package com.ly.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ly.common.core.bean.R;
import com.ly.common.core.bean.dto.CommonBatchUpdateDTO;
import com.ly.system.system.bean.entity.SysTenant;
import com.ly.system.service.ISysTenantService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 租户
 *
 * @author jeecg
 */
@Tag(name = "租户")
@Slf4j
@RestController
@RequestMapping("/sys/tenant")
@RequiredArgsConstructor
public class SysTenantController {

    private final ISysTenantService sysTenantService;

    /**
     * 查询-id
     *
     * @param id
     * @return
     */
    @Operation(summary = "查询-id")
    @GetMapping("/getId")
    public R<SysTenant> getId(@RequestParam(name = "id") String id) {
        return sysTenantService.getId(id);
    }

    /**
     * 分页
     *
     * @param entity
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Operation(summary = "分页")
    @GetMapping("/getPage")
    public R<IPage<SysTenant>> getPage(
            SysTenant entity,
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        return sysTenantService.getPage(entity, new Page<SysTenant>(pageNo, pageSize));
    }

    /**
     * 新增
     *
     * @param entity
     * @return
     */
    @Operation(summary = "新增")
    @PostMapping("/add")
    public R<String> add(@RequestBody SysTenant entity) {
        return sysTenantService.add(entity);
    }

    /**
     * 修改
     *
     * @param entity
     * @return
     */
    @Operation(summary = "修改")
    @PostMapping("/edit")
    public R<String> edit(@RequestBody SysTenant entity) {
        return sysTenantService.edit(entity);
    }

    /**
     * 删除-id
     *
     * @param id
     * @return
     */
    @Operation(summary = "删除-id")
    @DeleteMapping("/delete")
    public R<String> delete(@RequestParam(name = "id") String id) {
        return sysTenantService.delete(id);
    }

    /**
     * 批量新增或修改
     *
     * @param dto
     * @return
     */
    @Operation(summary = "批量新增或修改")
    @PostMapping("/updateBatch")
    public R updateBatch(@Validated @RequestBody CommonBatchUpdateDTO<SysTenant> dto) {
        return sysTenantService.updateBatch(dto);
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @Operation(summary = "批量删除")
    @DeleteMapping("/deleteBatch")
    public R<String> deleteBatch(@RequestParam(name = "ids") String ids) {
        return sysTenantService.deleteBatch(ids);
    }

    /**
     * 租户回收站
     *
     * @param sysTenant
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("/getRecyclePage")
    public R<IPage<SysTenant>> getRecyclePage(
            SysTenant sysTenant,
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        Page<SysTenant> page = new Page<>(pageNo, pageSize);
        return sysTenantService.getRecyclePage(page, sysTenant);
    }

    /**
     * 获取租户列表
     *
     * @return
     */
    @GetMapping("/getCurrentUserTenant")
    public R<List<SysTenant>> getCurrentUserTenant() {
        return sysTenantService.getCurrentUserTenant();
    }

    /**
     * 移除租户用户
     *
     * @param userIds
     * @param tenantId
     * @return
     */
    @PutMapping("/deleteTenantUser")
    public R<String> deleteTenantUser(
            @RequestParam("userIds") String userIds,
            @RequestParam("tenantId") String tenantId) {
        return R.ok();
    }

    /**
     * 彻底删除租户
     *
     * @param ids
     * @return
     */
    @DeleteMapping("/deleteLogicTenant")
    public R<String> deleteLogicTenant(@RequestParam("ids") String ids) {
        return sysTenantService.deleteLogicTenant(ids);
    }

    /**
     * 还原租户
     *
     * @param ids
     * @return
     */
    @PutMapping("/revertLogicTenant")
    public R<String> revertLogicTenant(@RequestParam("ids") String ids) {
        return sysTenantService.revertLogicTenant(ids);
    }

    /**
     * 变更租户拥有者【低代码应用专用接口】
     *
     * @param userId
     * @return
     */
    @PostMapping("/changeOwenUserTenant")
    public R<String> changeOwenUserTenant(@RequestParam("userId") String userId, @RequestParam("tenantId") String tenantId) {
        sysTenantService.changeOwenUserTenant(userId, tenantId);
        return R.ok("退出租户成功");
    }
}
